package com.shms.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfBuilds;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShBuild;
import com.shms.service.ShBuildService;

public class BuildsAction extends ActionSupport{
	private static final long serialVersionUID = -7106008904232245508L;
	private ShBuildService shBuildService;
	public GroupOfBuilds groupOfBuilds;
	public ShBuild shBuild;
	private Integer[] nos;
	public String message;
	/**
	 * 根据分页信息，查询宿舍楼列表
	 * @return 
	 */
	public String queryBuilds() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询动态信息的分页信息输入参数
		GroupOfBuilds buildsOfTrendsParams = new GroupOfBuilds();
		buildsOfTrendsParams.setCurrentPage(currentPage);
		buildsOfTrendsParams.setType(type);
		buildsOfTrendsParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfBuilds = shBuildService.queryBuilds(buildsOfTrendsParams);
		if (groupOfBuilds.getShBuild() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	
	/**
	 * 批量删除宿舍楼信息
	 * @return 
	 */
	public String deleteBuilds() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			ShBuild shBuild =new ShBuild();
			shBuild.setBid(Long.parseLong(ServletActionContext.getRequest().getParameter("no")));
			if(shBuildService.queryBuildsById(shBuild) == false){
				message="该宿舍楼已经存在宿舍信息，不允许删除";
				//获取传来的当前页数
				Integer currentPage = groupOfBuilds.getCurrentPage() - 1;
				//获取传来的上一页或下一页标志
				String type = groupOfBuilds.getType();
				//构造查询动态信息的分页信息输入参数
				GroupOfBuilds groupOfBuildsParams = new GroupOfBuilds();
				groupOfBuildsParams.setCurrentPage(currentPage);
				groupOfBuildsParams.setType(type);
				groupOfBuildsParams.setPageSize(QueryConstant.F_Pagesize);
				//查询
				groupOfBuilds = shBuildService.queryBuilds(groupOfBuildsParams);
				return INPUT;
			}
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shBuildService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfBuilds.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfBuilds.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfBuilds groupOfBuildsParams = new GroupOfBuilds();
			groupOfBuildsParams.setCurrentPage(currentPage);
			groupOfBuildsParams.setType(type);
			groupOfBuildsParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfBuilds = shBuildService.queryBuilds(groupOfBuildsParams);
			return SUCCESS;
		}else{
			message = "删除宿舍楼信息失败";
			return INPUT;
		}
	}
	/**
	 * 添加宿舍楼详细信息
	 * @return
	 */
	public String addBuilds(){
		return SUCCESS;
	}
	/**
	 * 保存宿舍楼详细信息
	 * @return
	 */
	public String saveBuilds() {
		boolean rtn = shBuildService.saveBuilds(shBuild);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		return SUCCESS;
	}
	/**
	 * 通过主键查询宿舍楼详细信息
	 * @return
	 */
	public String queryByBuildId() {
		//获取要查询的单条记录id
		long bid =  Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShBuild shBuildParams = new ShBuild();
		shBuildParams.setBid(bid);
		shBuild = shBuildService.queryById(shBuildParams);
		return SUCCESS;
	}
	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
	public ShBuildService getShBuildService() {
		return shBuildService;
	}
	public void setShBuildService(ShBuildService shBuildService) {
		this.shBuildService = shBuildService;
	}
	public GroupOfBuilds getGroupOfBuilds() {
		return groupOfBuilds;
	}
	public void setGroupOfBuilds(GroupOfBuilds groupOfBuilds) {
		this.groupOfBuilds = groupOfBuilds;
	}
	public ShBuild getShBuild() {
		return shBuild;
	}
	public void setShBuild(ShBuild shBuild) {
		this.shBuild = shBuild;
	}

	public void setMessage(String message) {
		this.message = message;
	}	

}
